import { Component, ChangeDetectionStrategy, OnInit, ChangeDetectorRef, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { RouteService } from '@core';
import { _HttpClient, ModalHelper, SettingsService } from '@delon/theme';
import { DomSanitizer } from '@angular/platform-browser';
import { NzMessageService, NzNotificationService } from 'ng-zorro-antd';
import { STComponent, STColumn } from '@delon/abc';
import { SysDictItemEditComponent } from './edit/sysdict-item-edit.component';

@Component({
  selector: 'app-sysdict-detail',
  templateUrl: './sysdict-item.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SysDictItemComponent implements OnInit {
  auth_btn_add = false;
  auth_btn_edit = false;
  auth_btn_del = false;

  constructor(
    route: ActivatedRoute,
    private routeStateSrv: RouteService,
    private router: Router,
    private http: _HttpClient,
    private cdr: ChangeDetectorRef,
    private dom: DomSanitizer,
    public msg: NzMessageService,
    private modal: ModalHelper,
    private notice: NzNotificationService,
    private settings: SettingsService,
  ) {
    this.params = route.snapshot.params;
  }
  dict: { [key: string]: any } = {};

  params: any;
  url = `/cf/admin/dict/item/page`;
  @ViewChild('st', { static: false }) st: STComponent;

  columns: STColumn[] = [
    { title: '字典key', index: 'type' },
    { title: '值', index: 'value' },
    { title: '标签名', index: 'label' },
    { title: '描述', index: 'description' },
    { title: '排序', index: 'sort' },
    { title: '备注', index: 'remarks' },
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          type: 'static',
          modal: { component: SysDictItemEditComponent, paramsName: 'record' },
          click: 'reload',
          iif: (item: STColumn) => {
            return this.auth_btn_edit && this.dict.system !== '1';
          },
        },
        {
          text: '删除',
          type: 'del',
          pop: {
            title: '确定删除字典？',
            okType: 'danger',
          },
          click: (record, _modal, comp) => {
            this.http.delete(`/cf/admin/dict/item/${record.id}`).subscribe(res => {
              if (res.status === 200) {
                this.notice.success('成功', `成功删除【${record.type}】`);
                comp!.load();
              }
            });
          },
          iif: (item: STColumn) => {
            return this.auth_btn_del && this.dict.system !== '1';
          },
        },
      ],
    },
  ];

  ngOnInit(): void {
    this.auth_btn_add = this.settings.user.permissions.sys_dict_add;
    this.auth_btn_edit = this.settings.user.permissions.sys_dict_edit;
    this.auth_btn_del = this.settings.user.permissions.sys_dict_del;

    this.http.get(`/cf/admin/dict/${this.params.dictId}`).subscribe(res => {
      if (res.status === 200) {
        this.dict = res.data;
      }
    });
  }

  back() {
    this.router.navigateByUrl(`/system/dict`, {
      state: { pi: this.routeStateSrv.pi, type: this.routeStateSrv.get('type') },
    });
  }

  add() {
    this.modal
      .createStatic(SysDictItemEditComponent, { i: { dictId: this.dict.id, type: this.dict.type } })
      .subscribe(() => this.st.reload());
  }

  reset() {
    this.params = { type: '' };
    this.st.load(1, this.params);
  }
}
